xquery version "3.0";

module namespace app="http://avto-magaz.ru/application/templates";

import module namespace templates="http://exist-db.org/xquery/templates" at "templates.xql";
import module namespace config="http://exist-db.org/xquery/apps/config" at "config.xqm";

(:~
 : This is a sample templating function. It will be called by the templating module if
 : it encounters an HTML element with a class attribute: class="app:test". The function
 : has to take exactly 3 parameters.
 : 
 : @param $node the HTML node with the class attribute which triggered this call
 : @param $model a map containing arbitrary data - used to pass information between template calls
 :)
declare function app:test($node as node(), $model as map(*)) {
    <p>Dummy template output generated by function app:test at {current-dateTime()}. The templating
        function was triggered by the class attribute <code>class="app:test"</code>.</p>
};

declare function app:categories($node as node(), $model as map(*)){
    <ul class="nav nav-list">{
        <li class="nav-header">категории</li>,
        for $cat in distinct-values(collection($config:app-root || "/data/all/goods/")//category)
        return <li>{if (request:get-parameter("cat", "") eq $cat) then attribute {"class"} { "active" } else ()}<a href="?cat={$cat}">{$cat}</a></li>
    }</ul>
};

declare %private function local:renrer-one($good){
    
        <li class="span3 masonry-brick">
            <div class="thumbnail">
                <img alt="" src="resources/good-placeholder.jpg"/>
                <div class="caption">
                    <h5>{$good/parameters/title/text()}</h5>
                    <p>{$good/comment/text()}</p>
                    <p>
                        <a class="btn primary details" href="#" rel="ajax/1.html">
                            <i class="icon-zoom-in"/>
                        </a>
                        <a class="btn addto" href="#" rel="1">Добавить <i class="icon-shopping-cart"/></a>
                    </p>
                    <p><span class="label label-info price">{$good/parameters/price/string()} руб.</span></p>
                </div>
            </div>
        </li>
};   

declare function app:models($node as node(), $model as map(*)){
    let $cat := request:get-parameter("cat", "")
    let $model := request:get-parameter("model", "")
    
    return if (not ($model)) then
        let $names := distinct-values(collection($config:app-root || "/data/all/goods/")//model[../category eq $cat])
        return 
            (:for $p in round(count($names) div 4):)
            (:return :)
            <div class="row-fluid">{
                for $name in $names (:subsequence($names, $p * 4 - 3, 4):)
                return 
                <div class="span3">
                    <a href="?cat={$cat}&amp;model={$name}">
                        {$name}
                    </a>
                </div>
            }</div>
            
    else if ($model) then
        let $cat := request:get-parameter("cat", "")
        let $goods := collection($config:app-root || "/data/all/goods/")//tovar[.//category eq $cat and .//model eq $model]
        return 
            (:for $p in round(count($goods) div 4)
            return :)
            <div class="row-fluid"><ul class="thumbnails masonry">{
                for $good in $goods (:subsequence($goods, $p * 4 - 3, 4):)
                return local:renrer-one($good)
            }</ul></div>
            
       else ()
};

